package com.wuyi.controller;

import com.github.pagehelper.PageInfo;
import com.wuyi.pojo.bo.*;
import com.wuyi.service.*;
import com.zc.enums.RoleEnum;
import com.zc.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName CenterUserController.java
 * @Description 用户个人管理controller
 * @createTime 2021年11月09日 20:39:00
 */
@Api(value = "个人管理",tags = {"用于个人管理的相关接口"})
@RestController
@RequestMapping("center")
public class CenterUserController {


    @Autowired
    private CenterUserService centerUserService;
    @Autowired
    private PostService postService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private FollowService followService;
    @Autowired
    private ResourceService resourceService;


    @ApiOperation(value = "用户信息模块---查询个人信息（已对接）", notes = "用户信息模块---查询个人信息（已对接）", httpMethod = "GET")
    @GetMapping("/queryUserInfo")
    public JSONResult queryUserInfo(@ApiParam(name = "userId", value = "用户id", required = true)
                                   @RequestParam String userId) {
        if (null == userId) {
            return JSONResult.errorMsg("userId不能为空");
        }
        return JSONResult.ok(centerUserService.queryUserInfo(userId));
    }

    @ApiOperation(value = "用户信息模块---修改个人信息（已对接）", notes = "修改个人信息（已对接）", httpMethod = "POST")
    @PostMapping("/updateUserInfo")
    public JSONResult updateUserInfo(@RequestBody CenterUserInfoBO userInfoBO) {

        if (null == userInfoBO.getUserId()) {
            return JSONResult.errorMsg("userId不能为空");
        }
        centerUserService.updateUserInfo(userInfoBO);
        return JSONResult.ok("修改成功");
    }


    @ApiOperation(value = "用户信息模块---修改个人头像（可对接）", notes = "用户信息模块---修改个人头像（可对接）", httpMethod = "POST")
    @PostMapping("/updateUserFace")
    public JSONResult updateUserFace(
                              @ApiParam(name = "userId", value = "当前用户Id" , required = true)
                              @RequestParam String userId,
                              @ApiParam(name = "images", value = "用户头像", required = true)
                              @RequestParam("images") MultipartFile images) throws IOException {
        //判空操作
        if (StringUtils.isBlank(userId)) {
            return JSONResult.errorMsg("userId不能为空");
        }
        if (images.isEmpty()) {
            return JSONResult.errorMsg("请上传您的头像");
        }
        String filesName = images.getOriginalFilename();
        //截取后缀名
        int fileOf = filesName.lastIndexOf(".");
        String fileType = filesName.substring(fileOf + 1);
        if (fileType.equals("jpg") || fileType.equals("png") || fileType.equals("jpeg") || fileType.equals("")) {

        }else{
            return JSONResult.errorMsg("图片格式不正确");
        }


        return centerUserService.updateUserFace(userId, images);
    }





    @ApiOperation(value = "用户信息模块---完善用户信息（已对接）", notes = "用户信息模块---完善用户信息（已对接）", httpMethod = "POST")
    @PostMapping("/complete")
    public JSONResult login(@RequestBody CenterUserInfoBO centerUserInfoBO) {

        //1.判空操作
        if (StringUtils.isBlank(centerUserInfoBO.getUserRealname())) {
            return JSONResult.errorMsg("真实姓名不能为空");
        }
        //判断学生的
        if (RoleEnum.STU.type.equals(centerUserInfoBO.getUserRole())) {
            if (StringUtils.isBlank(centerUserInfoBO.getStSchool())) {
                return JSONResult.errorMsg("学校不能为空");
            }
            if (StringUtils.isBlank(centerUserInfoBO.getStCollege())) {
                return JSONResult.errorMsg("学院不能为空");
            }
            if (StringUtils.isBlank(centerUserInfoBO.getStClass())) {
                return JSONResult.errorMsg("班级不能为空");
            }
            if (StringUtils.isBlank(centerUserInfoBO.getStNo())) {
                return JSONResult.errorMsg("学号不能为空");
            }
        }
        //判断老师的
        if (RoleEnum.TEC.type.equals(centerUserInfoBO.getUserRole())) {
            if (StringUtils.isBlank(centerUserInfoBO.getStSchool())) {
                return JSONResult.errorMsg("学校不能为空");
            }
            if (StringUtils.isBlank(centerUserInfoBO.getStCollege())) {
                return JSONResult.errorMsg("学院不能为空");
            }
        }
        // 2. 进行完善
        centerUserService.updateUserInfo(centerUserInfoBO);

        return JSONResult.ok("完善信息成功");

    }


    @ApiOperation(value = "用户信息模块---修改密码（可调试）", notes = "用户信息模块---修改密码（可调试）", httpMethod = "POST")
    @PostMapping("/changePsd")
    public JSONResult changePsd(@RequestBody UserPwdBO psdBO) throws Exception {

        if (StringUtils.isBlank(psdBO.getUserId())) {
            return JSONResult.errorMsg("用户ID不能为空");
        }
        if (StringUtils.isBlank(psdBO.getOldPwd())) {
            return JSONResult.errorMsg("原密码不能为空");
        }
        if (StringUtils.isBlank(psdBO.getNewPwd())) {
            return JSONResult.errorMsg("新密码不能为空");
        }
        if (psdBO.getNewPwd().length() < 6) {
            return JSONResult.errorMsg("新密码不能小于6位");
        }
        if (StringUtils.isBlank(psdBO.getNewPwdAgain())) {
            return JSONResult.errorMsg("确认密码不能为空");
        }
        if (!psdBO.getNewPwd().equals(psdBO.getNewPwdAgain())) {
            return JSONResult.errorMsg("两次密码输入不一致");
        }
        return centerUserService.changePsd(psdBO);
    }


    @ApiOperation(value = "帖子管理模块---查询帖子个人列表（已对接）", notes = "帖子管理模块---查询帖子个人列表（已对接）", httpMethod = "GET")
    @GetMapping("/queryUserPostInfo")
    public JSONResult queryUserPostInfo(@ApiParam(name = "userName", value = "用户名", required = true)
                                        @RequestParam String userName,
                                        @ApiParam(name = "postTitle", value = "帖子标题", required = false)
                                        @RequestParam String postTitle,
                                        @ApiParam(name = "postTime", value = "发布时间", required = false)
                                        @RequestParam String postTime,
                                        @RequestParam( required = false,defaultValue = "1")Integer pageNum,
                                        @RequestParam( required = false,defaultValue = "10")Integer pageSize) throws ParseException {

        if (StringUtils.isBlank(userName)) {
            return JSONResult.errorMsg("userName不能为空");
        }

        PageInfo pageInfo = centerUserService.queryUserPostInfo(userName, postTitle, postTime, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    @ApiOperation(value = "帖子管理模块---删除帖子（已对接）", notes = "帖子管理模块---删除帖子（已对接）", httpMethod = "GET")
    @GetMapping("/deleteUserPost")
    public JSONResult delete(@ApiParam(name = "postId", value = "帖子Id", required = true)
                             @RequestParam Integer postId) {

        if (null == postId) {
            return JSONResult.errorMsg("postId不能为空");
        }
        postService.deletePost(postId);
        return JSONResult.ok("删除帖子成功");
    }


    @ApiOperation(value = "关注和粉丝模块--查询关注/粉丝列表（可对接）", notes = "关注和粉丝模块--查询关注/粉丝列表（可对接）", httpMethod = "GET")
    @GetMapping("/queryFollowList")
    public JSONResult queryFollowList(@ApiParam(name = "userName", value = "当前登录用户名", required = true)
                                      @RequestParam String userName,
                                      @ApiParam(name = "type", value = "0：为查询我关注的人  1：查询我的粉丝", required = true)
                                      @RequestParam Integer type,
                                      @RequestParam( required = false,defaultValue = "1")Integer pageNum,
                                      @RequestParam( required = false,defaultValue = "10")Integer pageSize) {
        return JSONResult.ok(followService.queryFollowList(userName, type, pageNum, pageSize));
    }



    @ApiOperation(value = "我的收藏模块---按分类查询列表（可调试）", notes = "我的收藏模块---按分类查询列表（可调试）", httpMethod = "GET")
    @GetMapping("/myColList")
    public JSONResult myColList(@ApiParam(name = "categoryId", value = "分类Id", required = true)
                              @RequestParam Integer categoryId,
                              @ApiParam(name = "userName", value = "登录用户userName", required = false)
                              @RequestParam String userName,
                              @RequestParam( required = false,defaultValue = "1")Integer pageNum,
                              @RequestParam( required = false,defaultValue = "10")Integer pageSize) {
        if (null == categoryId) {
            return JSONResult.errorMsg("categoryId不能为空");
        }
        if (null == userName) {
            return JSONResult.errorMsg("userName不能为空");
        }

        PageInfo pageInfo = centerUserService.myColList(categoryId, userName, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }







    @ApiOperation(value = "资源管理模块--按条件查询个人发布资源列表（可对接）", notes = "资源管理模块--按条件查询个人发布资源列表（可对接）", httpMethod = "GET")
    @GetMapping("/queryResList")
    public JSONResult queryResList(@ApiParam(name = "userName", value = "当前登录用户名", required = true)
                                   @RequestParam String userName,
                                   @ApiParam(name = "resourceName", value = "要查询的资源名称", required = false)
                                   @RequestParam String resourceName,
                                   @ApiParam(name = "categoryId", value = "1：为资料  2：为课件  3：为软件", required = true)
                                   @RequestParam Integer categoryId,
                                   @RequestParam( required = false,defaultValue = "1")Integer pageNum,
                                   @RequestParam( required = false,defaultValue = "10")Integer pageSize) {

        if (StringUtils.isBlank(userName)) {
            return JSONResult.errorMsg("userName不能为空");
        }
        if (null == categoryId) {
            return JSONResult.errorMsg("categoryId不能为空");
        }

        PageInfo pageInfo = resourceService.queryResList(userName, resourceName, categoryId, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }


    @ApiOperation(value = "资源管理模块--删除个人发布资源（可对接）", notes = "资源管理模块--删除个人发布资源（可对接）", httpMethod = "POST")
    @PostMapping("/delRes")
    public JSONResult delRes(@RequestBody CenterResDelBO resDelBO) {

        if (StringUtils.isBlank(resDelBO.getUserName())) {
            return JSONResult.errorMsg("userName不能为空");
        }
        if (null == resDelBO.getResourceId()) {
            return JSONResult.errorMsg("resourceId不能为空");
        }

        return JSONResult.ok(resourceService.delRes(resDelBO));
    }






}
